<template>
  <div>
    <article class="weui_article">
      <div style="text-align: center;">
        <el-button type="primary" style="width: 100px;" :loading=butload @click="saveinfo">保存</el-button>
        <el-button type="info" style="width: 100px;" @click="runback">返回</el-button>
      </div>
      <b>材料信息</b>
      <hr/>
      <span>位置</span>
      <el-input v-model="dataContent.addr" placeholder="位置"></el-input>
      <span>材料名称/工种名称</span>
      <el-input v-model="dataContent.cname" placeholder="材料名称/工种名称"></el-input>
      <div style="margin-top: 5px;">
        <el-radio-group v-model="dataContent.type">
          <el-radio-button label="1">材料</el-radio-button>
          <el-radio-button label="2">工种</el-radio-button>
        </el-radio-group>
      </div>
      <span v-if="dataContent.type=='1'">品牌</span>
      <el-input v-model="dataContent.pp" v-if="dataContent.type=='1'" placeholder="品牌"></el-input>
      <span>单价(元)</span>
      <el-input v-model="dataContent.dj" placeholder="单价"></el-input>
      <span v-if="dataContent.type=='1'">材料费用(元)</span>
      <el-input v-model="dataContent.clfy" v-if="dataContent.type=='1'" placeholder="材料费用"></el-input>
      <span>人工费(元)</span>
      <el-input v-model="dataContent.rgf" placeholder="人工费"></el-input>
      <span>额外费用(元)</span>
      <el-input v-model="dataContent.ewf" placeholder="额外费用"></el-input>
      <span>已付款金额(元)</span>
      <el-input v-model="dataContent.yfkje" placeholder="已付款金额"></el-input>
      <span>总和费用(元)</span>
      <el-input v-model="dataContentzh" placeholder="总和费用"></el-input>
      <span>状态</span>
      <div>
        <el-radio-group v-model="dataContent.isfk">
          <el-radio-button label="0">未付款</el-radio-button>
          <el-radio-button label="1">已付款</el-radio-button>
          <el-radio-button label="2">付款中</el-radio-button>
        </el-radio-group>
      </div>
      <span>是否到货</span>
      <div>
        <el-radio-group v-model="dataContent.isdh">
          <el-radio-button label="0">未到货</el-radio-button>
          <el-radio-button label="1">已到货</el-radio-button>
        </el-radio-group>
      </div>
      <span>备注</span>
      <el-input type="textarea" v-model="dataContent.bz" :rows="2" placeholder="备注"></el-input>
      <br/>
      <b>联系人信息</b>
      <hr/>
      <span>联系人名称</span>
      <el-input v-model="dataConfig.username" placeholder="联系人名称"></el-input>
      <span>联系人手机号码</span>
      <el-input v-model="dataConfig.userphone" placeholder="联系人手机号码"></el-input>
      <span>备注</span>
      <el-input type="textarea" v-model="dataConfig.bz" :rows="2" placeholder="备注"></el-input>
      <div style="margin-top: 5px; text-align: center;">
        <el-button type="primary" style="width: 100px;" :loading=butload @click="saveinfo">保存</el-button>
        <el-button type="info" style="width: 100px;" @click="runback">返回</el-button>
      </div>
    </article>
  </div>
</template>
<script>
  import {
    Alert
  } from 'vux'

  export default {
    components: {Alert},
    data() {
      return {
        guid: '',
        dataContent: {
          guid: '',
          addr: '',
          cid: '',
          cname: '',
          type: '1',
          pp: '',
          dj: 0,
          clfy: 0,
          rgf: 0,
          ewf: 0,
          bz: '',
          zh: 0,
          temp1: '',
          temp2: '',
          temp3: '',
          isfk: '0',
          yfkje: 0,
          isdh: '0'
        },
        dataConfig: {
          guid: '',
          type: 'LX',
          key: '',
          value: '',
          username: '',
          userphone: '',
          bz: '',
          infoguid: ''
        },
        alertMessage: '',
        alerttype: 'info',
        butload: false
      }
    },
    created() {
      this.dataContent.guid = this.uuid();
      this.dataConfig.infoguid = this.dataContent.guid;
      this.dataConfig.guid = this.uuid();
    },
    computed:{
      dataContentzh:function(){
        this.dataContent.zh=parseInt(this.dataContent.rgf)+parseInt(this.dataContent.clfy)+parseInt(this.dataContent.ewf);
        return this.dataContent.zh;
      }
    },
    mounted() {
      console.log(this.dataContent);
      console.log(this.dataConfig);
    },
    methods: {
      saveinfo() {
        let _self = this;
        let apiurllet = '';
        apiurllet = global.baseApiUrl + '/ZXXX/insertZInfo';
        _self.butload = true;
        _self.axios({
          method: 'post',
          url: apiurllet,
          data: _self.dataContent
        }).then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            let apiconfig = global.baseApiUrl + '/ZXXX/insertZConfig';
            _self.dataConfig.infoguid = _self.dataContent.guid;
            console.log(_self.dataContent);
            console.log(_self.dataConfig);
            _self.axios({
              method: 'post',
              url: apiconfig,
              data: _self.dataConfig
            }).then(function (response) {
              if (response.data.code == 200) {
                //添加提示
                _self.alerttype = "success";
                _self.alertMessage = "保存成功";
                _self.message();
                _self.butload = false;
                _self.$router.replace({path: '/zxxx/zxxxinfolist'});
              } else {
                _self.alerttype = "error";
                _self.alertMessage = "数据提交异常，请重新尝试";
                _self.message();
                _self.butload = false;
                return false;
              }
            }).catch(function (response) {
              console.log(response);
            })
          }
          else {
            _self.alerttype = "error";
            _self.alertMessage = "数据提交异常，请重新尝试";
            _self.message();
            _self.butload = false;
            return false;
          }
        }).catch(function (response) {
          console.log(response);
        })
      },
      onHide: function () {
        if (this.alertMessage == "保存成功") {
          this.alertShow = true;
          this.alertMessage = "保存成功";
          this.$router.replace({path: '/zxxx/zxxxinfolist'});
        }
      },
      open() {
        this.$alert(this.alertMessage, '提示', {
          confirmButtonText: '确定',
          center: true,
          type: this.alerttype,
          callback: action => {
            this.$message({
              type: 'info',
              message: `消息: ${ this.alertMessage }`
            });
          }
        });
      },
      message() {
        this.$message({
          type: this.alerttype,
          message: `消息: ${ this.alertMessage }`
        });
      },
      runback() {
        this.$router.replace({path: '/zxxx/zxxxinfolist'});
      },
      uuid() {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
          s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";

        var uuid = s.join("");
        return uuid;
      }
    }
  }
</script>
<style rel="stylesheet/less" lang="less" scoped>
  .weui_article {
    h1 {
      text-align: center;
    }
    .post_time_source {
      text-align: center;
      font-size: 12px;
      color: #888;
      margin-bottom: 5px;
    }
  }
</style>
